package com.ws.shop.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;

import org.apache.log4j.Logger;

public class Tools {
	public static Logger log = null;
	static {
		log = Logger.getLogger(Tools.class);
	}

	/**
	 * 字符串处理
	 * 
	 * @param strIn
	 * @return
	 */
	public static String trimStr(String strIn) {
		StringBuffer strOut = new StringBuffer();
		char ch;
		for (int i = 0; i < strIn.length(); i++) {
			ch = strIn.charAt(i);
			if (ch != '\n')
				strOut.append(ch);
		}
		return strOut.toString();
	}

	public static String getRandomStr() {
		char[] takeArr = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
				'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', '0',
				'1', '2', '3', '4', '5', '6', '7', '8', '9', 'l', 'm', 'n',
				'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
				'0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
		char[] result = new char[8];
		for (int i = 0, j = 56; i < 8; ++i, --j) {
			int take = (int) (Math.random() * j);
			result[i] = takeArr[take];
			char m = takeArr[j - 1];
			takeArr[j - 1] = takeArr[take];
			takeArr[take] = m;
		}
		return new String(result);
	}

	public static String[] UnixCommand(String command) {
		String[] result = new String[2];
		try {
			Runtime run = Runtime.getRuntime();
			Process process = null;
			int r = -1;
			try {
				process = run.exec(command);
				r = process.waitFor();
			} catch (Exception e) {
				result[0] = "8";
				result[1] = e.toString();
				return result;
			}

			String exitValue = "" + r;

			ByteArrayOutputStream bao = new ByteArrayOutputStream();

			InputStream is = null;
			if (process.exitValue() != 0) {
				is = process.getErrorStream();
			} else
				is = process.getInputStream();
			byte[] buf = new byte[128];
			int readed;
			while ((readed = is.read(buf)) > 0)
				bao.write(buf, 0, readed);
			String allStr = bao.toString();
			result[0] = exitValue;
			result[1] = allStr;
			return result;
		} catch (Exception ex) {
			result[0] = "9";
			result[1] = ex.toString();
			return result;
		}
	}

	public static String getStackTrace(Exception e) {
		StringWriter sw = new StringWriter();
		PrintWriter pw = new PrintWriter(sw);
		e.printStackTrace(pw);
		return sw.toString();
	}

	public static boolean moveFile(String srcFile, String targetFile) {
		File file = new File(srcFile);
		if (file.exists()) {
			StringBuffer sb = new StringBuffer();
			sb.append("mv ");
			sb.append(srcFile);
			sb.append(" ");
			sb.append(targetFile);
			String[] result = Tools.UnixCommand(sb.toString());
			if (!result[0].equals("0")) {
				StringBuffer strError = new StringBuffer();
				strError.append("mv err,srcFile=");
				strError.append(srcFile);
				strError.append(" targetFile=");
				strError.append(targetFile);
				strError.append(" errdesc=");
				strError.append(result[1]);
				log.error(strError.toString());
				return false;
			}
			return true;
		} else {
			return false;
		}
	}

	public static boolean linkFile(String srcFile, String targetFile) {
		File file = new File(srcFile);

		if (file.exists()) {
			StringBuffer sb = new StringBuffer();
			sb.append("link ");
			sb.append(srcFile);
			sb.append(" ");
			sb.append(targetFile);
			String[] result = Tools.UnixCommand(sb.toString());
			if (!result[0].equals("0")) {
				StringBuffer strError = new StringBuffer();
				strError.append("link err,srcFile=");
				strError.append(srcFile);
				strError.append(" targetFile=");
				strError.append(targetFile);
				strError.append(" errdesc=");
				strError.append(result[1]);
				log.error(strError.toString());
				return false;
			}
			return true;
		} else {
			return false;
		}
	}

	public static boolean checkDate(String date, int len) {
		try {
			if (date.length() != len) {
				return false;
			}
			Integer.parseInt(date.substring(0, 4));
			int month = Integer.parseInt(date.substring(4, 6));
			if (month < 1 || month > 12) {
				return false;
			}
			int day = Integer.parseInt(date.substring(6, 8));
			if (day < 1 || day > 31) {
				return false;
			}
			if (len == 14) {
				int hour = Integer.parseInt(date.substring(8, 10));
				if (hour < 0 || hour > 23) {
					return false;
				}
				int min = Integer.parseInt(date.substring(10, 12));
				if (min < 0 || min > 59) {
					return false;
				}
				int sec = Integer.parseInt(date.substring(12, 14));
				if (sec < 0 || sec > 59) {
					return false;
				}
			}
		} catch (Exception ex) {
			return false;
		}
		return true;
	}

}
